package com.cgc.venus.server.app.bean;

import com.cgc.venus.server.core.utils.StringUtil;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * Created by 业哥 on 2017/11/16.
 * 应用管理-kubernetes 资源对象配置
 */
@Component
@ConfigurationProperties(prefix = "harbor")
public class AppManageConfigBean {

    public static final String Kind_Namespace = "kind: Namespace";

    public static final String Kind_Node = "kind: Node";

    public static final String Kind_Pod = "kind: Pod";

    public static final String Kind_Service = "kind: Service";

    public static final String Kind_PersistentVolume = "kind: PersistentVolume";

    public static final String Kind_PersistentVolumeClaim = "kind: PersistentVolumeClaim";

    public static final String Kind_Deployment = "kind: Deployment";

    public static final String Kind_Secret = "kind: Secret";

    public static final String Kind_StatefulSet = "kind: StatefulSet";

    public static final String Kind_ServiceAccount = "kind: ServiceAccount";

    public static final String Kind_ReplicaSet = "kind: ReplicaSet";

    public static final String Kind_ReplicationController = "kind: ReplicationController";

    public static final String Kind_Job = "kind: Job";

    public static final String Kind_Ingress = "kind: Ingress";

    public static final String Kind_ConfigMap = "kind: ConfigMap";

    public static final String Kind_HorizontalPodAutoscaler = "kind: HorizontalPodAutoscaler";

    public static final String Kind_NetworkPolicy = "kind: NetworkPolicy";

    public static final String Kind_DaemonSet = "kind: DaemonSet";

    public static final String APP_STATUS_RUNNING = "运行中";
    public static final String APP_STATUS_RUNNING_MESSAGE = "应用正常运行中";
    public static final String APP_STATUS_CLOSE = "已关闭";
    public static final String APP_STATUS_CLOSE_MESSAGE = "应用已经关闭,没有实例运行";
    public static final String APP_STATUS_ERROR = "异常";
    public static final String APP_STATUS_ERROR_MESSAGE = "应用异常,一个或多个容器出错,请到管理页面查看详情";
    public static final String APP_STATUS_CREATEING = "创建中...";
    public static final String APP_STATUS_CREATEING_MESSAGE = "";
    public static final String APP_POD_STATUS_ERROR = "此容器已经出错";
    public static final String APP_POD_STATUS_CREATEING = "kubernetes已经将容器分配到节点上，正在等待容器创建";


    public static final String APP_STATUS_RUNNING_EN = "running";
    public static final String APP_STATUS_CLOSE_EN = "close";
    public static final String APP_STATUS_ERROR_EN = "error";
    public static final String APP_STATUS_CONTAINER_CREATEING = "ContainerCreating";




    public static String getValueFromYaml(String yamlContent,String firstKey,String secondKey) {
        int kind_index = yamlContent.indexOf(firstKey);
        if (kind_index == -1) {
            return null;
        }
        // 先从king处截取
        yamlContent = yamlContent.substring(kind_index);
        int index = yamlContent.indexOf(secondKey);
        yamlContent = yamlContent.substring(0,index);
        return yamlContent;

    }



    // Harbor 镜像仓库地址
    private String baseUrl ;

    public String getBaseUrl() {
        return baseUrl;
    }

    public void setBaseUrl(String baseUrl) {
        this.baseUrl = baseUrl;
    }

    // 得到harbor 镜像仓库的地址
    public String getHarborUrl() {
        return getBaseUrl().replace("http://","").replaceAll("/api","");
    }

    // 得到完整的镜像名称
    public static String getImageName(String imageName,String version,String harborUrl) {
        return harborUrl + "/" + imageName + ":" + version;
    }

    // 得到应用自动伸缩资源对象的名称
    // 默认是:当前的应用名称+ -hpa
    public static String getHpaName(String appNameKube) {
        return appNameKube + "-hpa";
    }


    /*
    * 镜像仓库类型
    * */
    public static String PRIVATE_WAREHOUSE = "private";
    public static String PUBLIC_WAREHOUSE = "public";
    public static String STORE_WAREHOUSE = "store";



    public static String appendStr(String[] arr) {
        StringBuilder sb = new StringBuilder();
        int arrLength = arr.length;
        for (int i = 0; i < arrLength; i++) {
            sb.append(arr[i].trim());
            if (i < arrLength - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    //////////////////////////////////////////根据镜像,部署应用,配置项///////////////////////////////////////

    // 应用标签key
    public static final String APP_LABEL_KEY = "app";

    // 应用标签,挂载目录
    public static final String MOUTH_PATH_KEY = "mountPath";

    // 物理分组key
    public static final String APP_LABEL_PHYSICAL_GROUP_KEY = "physical.group";

    // 拉取镜像秘钥key
    public static final String PULL_IMAGE_SECRET = "harbor-default";


    // 容器端口名称,前缀,不能超过15个字符
    public static final String CONTAINER_PORT_NAME = "port-";

    // 配置文件卷名称,前缀
    public static final String CONFIGMAP_VOLUME_NAME = "configmap-volume-";

    // 持久化卷名称,前缀
    public static final String STATEFULSET_PVC_VOLUME_NAME = "statefulset-volume-";

    // 持久化卷名称,前缀
    public static final String DEPLOYMENT_PVC_VOLUME_NAME = "deployment-volume-";

    // 持久化卷,key
    public static final String ANNOEATIONS_Map_KEY = "volume.beta.kubernetes.io/storage-class";

    // 动态存储类名称
    public static final String ANNOEATIONS_Map_VALUE = "managed-nfs-storage";

    // CPU,内存,单位
    public static final String Mi = "Mi";

    public static final String Gi = "Gi";


    // 异常信息

    public static final String APP_DEPLOY_EXCEPTION_NOT_FOUND_PHYSICAL_GROUP = ":空间,没有分配物理分组,请选择其他部署空间";

   /*
   * 得到service服务名称
   * */
    public static String getServiceName(String appNameKube) {
        //return appNameKube + "-" +(int)((Math.random()*9+1)*10000) + "-service";
        return appNameKube + "-" +StringUtil.getRandomString(5) + "-service";
    }

    // 动态生成端口类型
    public static final String SERVICE_PORT_TYPE_DYNAMIC = "dynamic";
    // 指定端口
    public static final String SERVICE_PORT_TYPE_APPOINT = "appoint";

    // nodeport
    public static final String SERVICE_PORT_TYPE_NODEPORT = "NodePort";


    public static final String CLUSTERIP = "ClusterIP";
    public static final String NONE = "None";

    public static final String VENUS_DOMAIN_NAME = ".cgc.me";

    // 服务类类型
    public static final String SERVICE_TYPE = "service-type";
    public static final String SERVICE_TYPE_IN = "in";
    public static final String SERVICE_TYPE_TCP = "tcp";
    public static final String SERVICE_TYPE_HTTP = "http";

    /*
    * 生成域名
    * 应用名称-空间名称.pn.5位的随机字符串.venus.com
    * */
    public static String createDomainName(String namespace,String appNameKube,int portIndex) {
        return appNameKube + "-" + namespace + ".p" + portIndex + "."+StringUtil.getRandomString(5)+ VENUS_DOMAIN_NAME;
    }










}
